package com.xyh.run.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.run.dao.UserMapperDao;
import com.xyh.run.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @version v1.0
 * @类描述： 自定义用户信息处理
 * @项目名称：
 * @包名： com.xyh.run.config
 * @类名称：MyUserDecode
 * @创建人：YIHANG.XU
 * @创建时间：2021/11/1110:23 上午
 * @修改人：YIHANG.XU
 * @修改时间：2021/11/1110:23 上午
 * @修改备注：
 * @Copyright
 */
@Slf4j
public class MyUserDetails implements UserDetailsService {
    @Autowired
    private UserMapperDao userMapperDao;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", userName);
        UserVo userVo = userMapperDao.selectOne(queryWrapper);
        if (userVo != null) {
            throw new UsernameNotFoundException("没有用户");
        }
        List<GrantedAuthority> admin = AuthorityUtils.createAuthorityList("admin");
        return new User(userVo.getName(), userVo.getPassword(), admin
        );
    }
}
